<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Sort extends Admin
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data = Db::table('l_sort')
                ->alias('ls')
                ->field('ls.*, lc.cinema, lf.filmname, ld.studio_name')
                ->join('l_cinema lc', 'ls.cinema_id=lc.id')
                ->join('l_film lf', 'ls.film_id=lf.id')
                ->join('l_studio ld', 'ls.studio_id=ld.id')
                ->select();

        return view('Sort/index', [
            'list'=>$data
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $film = Db::table('l_film')->select();
        $cinema = Db::table('l_cinema')->select();
        $n = date('n');

        return view('Sort/add', [
            'film'=>$film,
            'cinema'=>$cinema,
            'n'=>$n
        ]);
    }

    /**
     * AJAX根据影院选择影厅联动
     */
    public function ajaxsort($id)
    {
        $data = Db::table('l_studio')->where('cinema_id',$id)->select();
        return $data;
    }

    /**
     * AJAX根据月份来选择天数
     */
    public function ajaxdate($id)
    {
        $y = date('Y');
        if ($id) {
            $n = $id;
        } else {
            $n = date('n');
        }
        // return cal_days_in_month(CAL_GREGORIAN, $n, $y);
        $dayCount = date("t",strtotime($y."-".$n));
        return $dayCount;
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $rel = $request->post();
        $film_y = date('Y');
        $film_n = $rel['time-n'];
        $film_j = $rel['time-j'];
        //去除分钟的前导零
        $i = substr($rel['time-i'], 1);
        //进行日期时间非法判断
        if ($rel['time-n'] == date('n')) {
            if ($rel['time-j'] < date('j')) {
                $this->error('天数不合法');
                exit;
                //时钟非法判断
                if ($rel['time-h'] < date('g')){
                    $this->error('时间不合法');
                    exit;
                }
                if ($rel['time-h'] == date('g')){
                    if ($i < date('i')) {
                        $this->error('时间不合法');
                        exit;
                    }
                }
            }
        }
        //查询影片时长
        $film_time = db('l_film')->field('time')->where('id', $rel['film'])->select()[0]['time'];

        //计算结束时 分
        $film_h_i = explode('.', $film_time);

        if ($film_h_i[0]+$rel['time-i'] > 120) {
            $film_h = $rel['time-h']+2;
            $film_i = $film_h_i[0]+$rel['time-i']-120;
        } elseif($film_h_i[0]+$rel['time-i'] > 60){
            $film_h = $rel['time-h']+1;
            $film_i = $film_h_i[0]+$rel['time-i']-60;
        } else {
            $film_h = $rel['time-h'];
            $film_i = $film_h_i[0]+$rel['time-i'];
        }
        //如果时钟超出24小时，天数加一
        if ($film_h > 24) {
            $film_j = $rel['time-j']+1;
            $film_h = $film_h-24;
        }
        //如果天数超过当前月份的天数 月份加一
        if ($film_j > cal_days_in_month(CAL_GREGORIAN, $film_n, date('Y'))) {
            $film_j = $film_j-cal_days_in_month(CAL_GREGORIAN, $film_n, date('Y'));
            $film_n = $film_n+1;
        }
        //如过月数超过12 年份加一
        if ($film_n > 12) {
            $film_n = $film_n-12;
            $film_y = $film_y+1;
        }
        //拼接开始时间
        $time = $film_y.'/'.$rel['time-n'].'/'.$rel['time-j'].' '.$rel['time-h'].':'.$i;
        //得出结束时间 年-月-日-时-分
        $overtime = $film_y.'/'.$film_n.'/'.$film_j.' '.$film_h.':'.$film_i;

        $data = [
            'film_id'=>$rel['film'],
            'cinema_id'=>$rel['cinema'],
            'studio_id'=>$rel['studio'],
            'time'=>$time,
            'overtime'=>$overtime,
            'price'=>$rel['price']
        ];

        $list = Db::table('l_sort')
                ->insert($data);
        if($list){
            $this->success('排片成功', 'admin/Sort/index');
        }else{
            $this->error('出现异常');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = Db::table('l_sort')
            ->where('id', $id)
            ->delete();

        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . ' 的场次删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . ' 的场次删除失败,请重试!';
        }
        return json($info);
    }
}
